package com.LachesismStorm.lachesism.member.service.impl;

import com.LachesismStorm.lachesism.core.common.response.ResponsePage;
import com.LachesismStorm.lachesism.core.service.RedisService;
import com.LachesismStorm.lachesism.core.util.LachesismTools;
import com.LachesismStorm.lachesism.member.mapper.YunOManageMapper;
import com.LachesismStorm.lachesism.member.service.YunOManageService;
import com.LachesismStorm.lachesism.member.vo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.LinkedList;
import java.util.List;

import static com.LachesismStorm.lachesism.core.common.enums.CommonEnum.FOOD_CACHE;

/**
 * @Author lachesism
 * @Date 2024-04-10 16:07
 **/
@Service
public class YunOManageServiceImpl implements YunOManageService {
    @Resource
    private YunOManageMapper yunOManageMapper;
    @Resource
    private RedisService redisService;


    @Override
    public void addFoodTypeName(String foodTypeName) {
        redisService.del(FOOD_CACHE);
        yunOManageMapper.addFoodTypeName(foodTypeName);
    }

    @Override
    public List<SysFoodMenu> getFoodTypeList() {
        List<SysFoodMenu> foodTypeList = (List<SysFoodMenu>) redisService.getWithTimeout(FOOD_CACHE);
        if (foodTypeList != null && foodTypeList.size() > 0) {
            return foodTypeList;
        }
        List<SysFoodMenu> foodTypeList2 = yunOManageMapper.getFoodTypeList();
        redisService.set(FOOD_CACHE, foodTypeList2);
        return yunOManageMapper.getFoodTypeList();
    }

    @Override
    public void accessLog(String path) {
        String ip = LachesismTools.getRealRequestIp();
        yunOManageMapper.addAccessLog(path, ip);
    }

    @Override
    public List<FileInfoVO> getFolderInfo(FileInfoVO fileInfo) {
        String path = "/project/media" + fileInfo.getPath();
        LinkedList<FileInfoVO> fileList = new LinkedList<>();
        try {
            File directory = new File(path);
            File[] files = directory.listFiles();
            if (files != null) {
                for (File file : files) {
                    String fileName = file.getName();
                    FileInfoVO fileInfoVO = new FileInfoVO();
                    fileInfoVO.setFileName(fileName);
                    fileInfoVO.setPath(path + fileName);
                    fileInfoVO.setUrl("https://www.yunox.cn/media" + fileInfo.getPath() + fileName);
                    fileInfoVO.setSize(LachesismTools.convertFileSize(file.length()));
                    fileInfoVO.setType(getFileType(file));
                    fileList.add(fileInfoVO);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileList;
    }

    @Override
    public Boolean deleteFile(FileInfoVO fileInfo) {
        String path = fileInfo.getPath();
        final File sampleFile = new File(path);
        if (sampleFile.exists()) {
            try {
                if (sampleFile.delete()) {
                    return true;
                } else {
                    // 即使文件存在，delete() 方法也可能返回 false
                    // 这可能是因为文件正在被使用或其它系统级的原因
                    System.err.println("文件存在，但删除失败: " + path);
                    return false;
                }
            } catch (SecurityException e) {
                // SecurityException 会在调用 delete() 时没有足够权限访问文件时抛出
                System.err.println("安全异常：无法删除文件 " + path + "。原因: " + e.getMessage());
                return false;
            }
        }
        System.err.println("文件不存在，无法删除: " + path);
        return false;
    }

    @Override
    public ResponsePage<ExtExceptionLog> getErrorLog(Integer pageNum) {
        PageHelper.startPage(pageNum, 30);
        List<ExtExceptionLog> extExceptionLogList = yunOManageMapper.getErrorLog();
        return ResponsePage.restPage(new PageInfo<>(extExceptionLogList));
//    return null;
    }

    @Override
    public void editSuggestion(SysSuggestions suggestion) {
        yunOManageMapper.editSuggestion(suggestion);
    }

    private String getFileType(File file) {
        String fileName = file.getName();
        if (fileName.contains(".")) {
            return fileName.substring(fileName.lastIndexOf('.') + 1);
        } else {
            return "文件";
        }
    }

    @Override
    public void addFood(String foodName) {
        yunOManageMapper.addFood(foodName);
    }

    @Override
    public void addMotto(StringInfoVO info) {
        yunOManageMapper.addMotto(info.getInfo());
    }


}
